package com.koushikdutta.ion.loader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.text.TextUtils;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.async.util.StreamUtility;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.bitmap.IonBitmapCache;
import com.koushikdutta.ion.gif.GifAction;
import com.koushikdutta.ion.gif.GifDecoder;
import java.io.InputStream;

public class StreamLoader extends SimpleLoader {
    protected BitmapInfo loadGif(String key, Point size, InputStream in, Options options) throws Exception {
        GifDecoder decoder = new GifDecoder(in, new GifAction() {
            public boolean parseOk(boolean parseStatus, int frameIndex) {
                return true;
            }
        });
        decoder.run();
        StreamUtility.closeQuietly(in);
        if (decoder.getFrameCount() == 0) {
            throw new Exception("failed to load gif");
        }
        Bitmap[] bitmaps = new Bitmap[decoder.getFrameCount()];
        int[] delays = decoder.getDelays();
        for (int i = 0; i < decoder.getFrameCount(); i++) {
            Bitmap bitmap = decoder.getFrameImage(i);
            if (bitmap == null) {
                throw new Exception("failed to load gif frame");
            }
            bitmaps[i] = bitmap;
        }
        BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmaps, size);
        info.delays = delays;
        return info;
    }

    protected InputStream getInputStream(Context context, String uri) throws Exception {
        return null;
    }

    public Future<BitmapInfo> loadBitmap(Context context, Ion ion, String key, String uri, int resizeWidth, int resizeHeight, boolean animateGif) {
        final SimpleFuture<BitmapInfo> ret = new SimpleFuture();
        final Context context2 = context;
        final String str = uri;
        final Ion ion2 = ion;
        final int i = resizeWidth;
        final int i2 = resizeHeight;
        final boolean z = animateGif;
        final String str2 = key;
        Ion.getBitmapLoadExecutorService().execute(new Runnable() {
            public void run() {
                InputStream in = null;
                try {
                    Object info;
                    Options options = ion2.getBitmapCache().prepareBitmapOptions(StreamLoader.this.getInputStream(context2, str), i, i2);
                    StreamUtility.closeQuietly(in);
                    Point size = new Point(options.outWidth, options.outHeight);
                    in = StreamLoader.this.getInputStream(context2, str);
                    if (z && TextUtils.equals("image/gif", options.outMimeType)) {
                        info = StreamLoader.this.loadGif(str2, size, in, options);
                    } else {
                        if (IonBitmapCache.loadBitmap(in, options) == null) {
                            throw new Exception("Bitmap failed to load");
                        }
                        info = new BitmapInfo(str2, options.outMimeType, new Bitmap[]{bitmap}, size);
                    }
                    info.loadedFrom = 1;
                    ret.setComplete(info);
                    StreamUtility.closeQuietly(in);
                } catch (OutOfMemoryError e) {
                    ret.setComplete(new Exception(e), null);
                    StreamUtility.closeQuietly(in);
                } catch (Exception e2) {
                    ret.setComplete(e2);
                    StreamUtility.closeQuietly(in);
                } catch (Throwable th) {
                    StreamUtility.closeQuietly(in);
                }
            }
        });
        return ret;
    }
}
